<?php
namespace app\index\controller;
use think\Db;
use think\Exception;

class Notify extends Base
{
    public function Index(){
        $data = $this->request->post();
        $host =$this->request->host();
//        if ($host!=config('myself_notify_url')){
//            return $this->sendFail($host." is unallow");
//        }
        try{
            $isPayment = "payment" == $data["notify_type"];
            if ($isPayment){
               return $this->payment($data);
            }else{
                return $this->payNotify($data);
            }
        }catch (\Exception $e){
            return $this->sendFail("异常：".$e->getMessage());
            die();
        }
        return $this->sendFail($host."notify_type:".$data["notify_type"]." isPayment=$isPayment");
    }

    /**
     * 充值情况回调
     */
    private function payNotify($data){
        $where = array();
        $where["transaction_no"] =$data["transaction_no"];
//        $where["wallet_id"]=$data["wallet_id"];
        $where["coin_symbol"]=$data["coin_symbol"];
        $where["to_address"]=$data["to_address"];
        $number=$data["amount"];
        $log = Db::table("tb_withdrawcoin_log")->where($where)->find();
        $amount= $log["amount"];
        if (!$log){
            return $this->sendFail(" tb_withdrawcoin_log null:". Db::table("tb_withdrawcoin_log")->getLastSql());
        }
        if ($log["status"]!=1){
            return $this->sendSuccess("此订单已收到通知",$log);
        }
        $notify_type= $data["notify_type"];
        //充值成功
        $updateData = array();
        $updateData["mark"]=json_encode($data);
        $isPayOk = $notify_type=="confirm";
        $id=$log['id'];
        $uid=$log['uid'];
        if ($isPayOk){
            $wallet = Db::table("tb_wallet")->where("uid",$uid)->where("coin_symbol",$data['coin_symbol'])->find();
            $updateData["status"]=2;
            $updateData["log"]="可用:".$wallet["number"]."冻结:".($wallet["frozen"]-abs($amount));
            $updateData["txid"]=$data['tx_id'];
        }else{
            //充值失败
            $updateData["status"]=3;
        }

        Db::startTrans();
        $updateDataRes = Db::table("tb_withdrawcoin_log")->where("id",$id)->update($updateData);
        if (!$updateDataRes){
            Db::rollback();
            return $this->sendFail("update withdrawcoin_log fail ");
        }

        //解冻
        $froze= Db::table("tb_wallet")->where("uid",$uid)->where("coin_symbol",$data['coin_symbol'])->setDec("frozen",abs($amount));
        if (!$froze){
            Db::rollback();
            return $this->sendFail("unfrozen fail");
        }
        moneyRecord($uid,abs($amount),$isPayOk?5:6,$isPayOk?"提币成功扣除":"提现失败解冻");
        if (!$isPayOk){
            //增加解除冻结余额
            $number= Db::table("tb_wallet")->where("uid",$uid)->where("coin_symbol",$data['coin_symbol'])->setInc("number",abs($amount));
            if (!$number){
                Db::rollback();
                return $this->sendFail("number add fail ");
            }
        }
        Db::commit();
        return $this->sendSuccess("ok");
    }

    /**
     * @param $data
     * 充值
     */
    private function payment($data){
        /**
         * 充值参数 $to_address 地址  $coin_symbol 币种  $amount 数量
         * */
        $from_address = $data["from_address"];
        $to_address = $data["to_address"];
        $coin_symbol = $data["coin_symbol"];
        $amount = $data["amount"];

        /**
         * 币种增加 数量
         */
        Db::startTrans();
        $userWallet = Db::table('tb_wallet')->where('coin_symbol', $coin_symbol)->where('address', $to_address)->find();
        $status = 0;
        $inc = false;
        if ($userWallet){
            $inc = Db::table('tb_wallet')->where('coin_symbol', $coin_symbol)->where('address', $to_address)->setInc('number', $amount);
        }
        $insertdata = array();
        if ($inc){
            $status =1;
            $insertdata['uid'] = $userWallet["uid"];
        }else{
            $insertdata['uid'] = 0;
        }
        $insertdata['create_time'] = time();
        $insertdata['status'] = $status;
        $insertdata['from_address'] = $from_address;
        $insertdata['to_address'] = $to_address;
        $insertdata['amount'] =$amount;
        $insertdata['coin_symbol'] = $coin_symbol;
        $insertdata['tx_id'] = $data["tx_id"];
        $insertdata['wallet_id'] = $data["wallet_id"];
        $insertdata['wallet_memo'] = $data["wallet_memo"];
        $insertdata['fee'] = $data["fee"];
        $insertdata['transaction_id'] = $data["transaction_id"];

        /**
         * 充值日志记录
         */
        $insertLog = Db::table('tb_wallet_log')->insert($insertdata);
        if ($insertLog){
            //$uid,$num,$type,$desc,$coin="USDT")
            moneyRecord($userWallet["uid"],$amount,2,"外部充值到账","USDT");
            Db::commit();
            return $this->sendSuccess('pay none');
        }else{
            Db::rollback();
            return $this->sendFail('wallet_log fail');
        }

    }


}